﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class backmanage_API_A_Fileupload : System.Web.UI.Page
{
    /// <summary>
    /// UPFILESYSTEM API 文件上传API
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
        HttpPostedFile file = files["fup"];
        string upType = Request.Params["uptype"];
        string saveUrl = string.Empty;
        switch (upType)
        {
            case "0":
                {
                    saveUrl = "../../Image/AD/";
                    break;
                }
            case "1":
                {
                    saveUrl = "../spreadsystem/spreaderimg/";
                    break;
                }
            case "2":
                {
                    saveUrl = "../../Image/Game/";
                    break;
                }
            default: break;
        }
        
        if (file.ContentLength <= Convert.ToInt32(50 * 1024 * 1024))
        {
            if (IsValidFileType(file.FileName))
            {
                string picPaht = Server.MapPath(saveUrl);
                Random rd = new Random();
                string name = DateTime.Now.ToString("yyyyMMddhhmmssfff") + rd.Next(1000) + GetExtension(file.FileName);
                string strNewPath = GetSaveFilePath(picPaht) + name;
                file.SaveAs(strNewPath);
                Response.Write("{msg:'" + saveUrl + name + "',err:''}"); ;
            }
            else
            {
                Response.Write("{msg:'',err:'类型错误!'}");
            }
        }
        else
        {
            Response.Write("{msg:'',err:'大小错误!'}");
        }
    }

    /// <summary>
    /// 验证文件类型
    /// </summary>
    /// <param name="FileName"></param>
    /// <returns></returns>
    private bool IsValidFileType(string FileName)
    {
        string imgType = "jpg|bmp|gif|png";
        string[] _AcceptedFileTypes = imgType.Split('|');
        string ext = FileName.Substring(FileName.LastIndexOf(".") + 1, FileName.Length - FileName.LastIndexOf(".") - 1);
        for (int i = 0; i < _AcceptedFileTypes.Length; i++)
        {
            if (ext == _AcceptedFileTypes[i])
            {
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// 获得文件扩展名
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    private string GetExtension(string fileName)
    {
        try
        {
            int startPos = fileName.LastIndexOf(".");
            string ext = fileName.Substring(startPos, fileName.Length - startPos);
            return ext;
        }
        catch
        {
            return string.Empty;
        }
    }

    /// <summary>
    /// 创建文件要保存的路径
    /// </summary>
    /// <param name="picPath">文件路径</param>
    /// <returns></returns>
    private string GetSaveFilePath(string picPath)
    {
        if (!Directory.Exists(picPath))
        {
            Directory.CreateDirectory(picPath);
        }
        return picPath;
    }
}
